package com.zjs.file.operation;

import java.io.File;

/**
 *该类用于修改文件拓展名
 */
public class ModifyFileName{

	public static void main(String[] args) {
		//ModifyFileName.modifyTheExtension("E:/迅雷下载","txt","mp4");
//		ModifyFileName.modifyTheExtension("E:/迅雷下载","mp4");
		ModifyFileName.modifyFileName("D:\\Download");
	}

	/**
	 * 该方法包含一个参数（路径）
	 * 修改文件名称，以日期命名
	 */
	public static void modifyFileName(String path){
		File file = new File(path);
		//得到文件夹下的所有文件和文件夹
		String[] list = file.list();
		if(list!=null && list.length>0){
			//获取当前日期
			long dateName = System.currentTimeMillis();
			for (String oldName : list) {
				File oldFile = new File(path,oldName);
				//判断出文件和文件夹
				if(!oldFile.isDirectory()){
					//文件则判断是否有拓展名
					if(oldName.contains(".")){
						String[] oldNameArr = oldName.split("\\.");
						String newoldName = (++dateName)+"."+oldNameArr[oldNameArr.length-1];
						File newFile = new File(path,newoldName);
						boolean flag = oldFile.renameTo(newFile);
						System.out.println(oldName+"修改为"+newoldName+"  结果:"+flag);
					}else {
						String newoldName = (++dateName)+"";
						File newFile = new File(path,newoldName);
						boolean flag = oldFile.renameTo(newFile);
						System.out.println(oldName+"修改为"+newoldName+"  结果:"+flag);
					}
				}
				else{
					//文件夹则迭代
					String newpath=path+"/"+oldName;
					modifyFileName(newpath);
				}
			}
		}
	}
	/**
	 * 该方法包含三个参数（路径，旧拓展名，新拓展名）
	 * 将修改拓展名为旧拓展名的文件拓展名修改为新拓展名
	 */
	public static void modifyTheExtension(String path,String oldExtension,String newExtension){
		File file = new File(path);
		//得到文件夹下的所有文件和文件夹
		String[] list = file.list();
		if(list!=null && list.length>0){
			for (String oldName : list) {
				File oldFile = new File(path,oldName);
				//判断出文件和文件夹
				if(!oldFile.isDirectory()){
					//文件则判断是不是要修改的
					if(oldName.contains("."+oldExtension)){
						String newoldName = oldName.substring(0, oldName.lastIndexOf("."))+"."+newExtension;
						File newFile = new File(path,newoldName);
						boolean flag = oldFile.renameTo(newFile);
						System.out.println(oldName+"修改为"+newoldName+"  结果:"+flag);
					}
				}
				else{
					//文件夹则迭代
					String newpath=path+"/"+oldName;
					modifyTheExtension(newpath,oldExtension,newExtension);
				}
			}
		}
	}
	/**
	 * 该方法包含两个参数（路径，新拓展名）
	 * 将全部文件的拓展名修改为新拓展名
	 */
	public static void modifyTheExtension(String path,String newExtension){
		File file = new File(path);
		//得到文件夹下的所有文件和文件夹
		String[] list = file.list();
		if(list!=null && list.length>0){
			for (String oldName : list) {
				File oldFile = new File(path,oldName);
				//判断出文件和文件夹
				if(!oldFile.isDirectory()){
					//文件则判断是否有拓展名
					if(!oldName.contains(".")){
						String newoldName = oldName+"."+newExtension;
						File newFile = new File(path,newoldName);
						boolean flag = oldFile.renameTo(newFile);
						System.out.println(oldName+"修改为"+newoldName+"  结果:"+flag);
					}else {
						System.out.println(oldName);
						String newoldName = oldName.split(".")[0]+"."+newExtension;
						File newFile = new File(path,newoldName);
						boolean flag = oldFile.renameTo(newFile);
						System.out.println(oldName+"修改为"+newoldName+"  结果:"+flag);
					}
				}
				else{
					//文件夹则迭代
					String newpath=path+"/"+oldName;
					modifyTheExtension(newpath,newExtension);
				}
			}
		}
	}
}


